var TP = window.TP || {};
//global
var isMobile = /iPad|iPod|iPhone|Android/.test(navigator.userAgent),
    isIos = /iPad|iPod|iPhone/.test(navigator.userAgent),
    isAndroid = /Android/.test(navigator.userAgent),
    $window = $(window),
    $body = $("body"),
    $commentId = $("input[name='commentId']").val(),
    $btnComment = $("#btnTPComment"),
    $btnCloseComment = $(".btn-closecomment"),
    $commentwrap = $body.find(".commentwrap"),
    $masker = $(".masker");
var winW = $window.width(),
    winH = $window.height(),
    randomNum = Math.floor(Math.random() * 1000);

//TP is in the window scope.
TP.comment_module = {
    init: function() {
        if (!isMobile) {
            $("body").addClass("desktop");
        }if (isIos){
            $("body").addClass("ios");
        }
        $btnComment.on("touchstart click",function(e){
            e.preventDefault();
            showComment();
            $(".commentwrap").addClass("comment-active");
            setTimeout(function () {
                $(".masker").addClass("masker-show");
            },100)

            $('#commframe').load(function() {
                $('.loadmask').remove();
            });
        })
        $body.on("touchstart click", ".btn-closecomment", function(e){
            e.preventDefault();
            var $this = $(this);
            $this.parent().removeClass("comment-active").next().removeClass("masker-show")
        })
        function showComment() {
            if ($commentwrap.has(".commentcon").length) {
                return false;
            }else if(isIos){
                //$commentwrap.append("<a href='#' class='btn-closecomment'>X</a><div class='commentcon'><div class='loadmask'></div><iframe id='commframe' width='"+winW+"' scrolling='no' frameborder='0' src='commt_page.html'></iframe></div>").after("<div class='masker'></div>");
                $commentwrap.append("<a href='#' class='btn-closecomment'>X</a><div class='commentcon'><div class='loadmask'></div><iframe id='commframe' width='"+winW+"' scrolling='no' frameborder='0' src='http://m.thepaper.cn/newDetail_commt_h5.jsp?contid="+$commentId+"&redirectUrl="+encodeURIComponent(location.href)+"&"+randomNum+"'></iframe></div>").after("<div class='masker'></div>");
            }else{
                //$commentwrap.append("<a href='#' class='btn-closecomment'>X</a><div class='commentcon'><div class='loadmask'></div><iframe id='commframe' width='100%' height='100%' frameborder='0' src='commt_page.html'></iframe></div>").after("<div class='masker'></div>");
                $commentwrap.append("<a href='#' class='btn-closecomment'>X</a><div class='commentcon'><div class='loadmask'></div><iframe id='commframe' width='100%' height='100%' frameborder='0' src='http://m.thepaper.cn/newDetail_commt_h5.jsp?contid="+$commentId+"&redirectUrl="+encodeURIComponent(location.href)+"'></iframe></div>").after("<div class='masker'></div>");
            }

            //$commentwrap.append("<a href='#' class='btn-closecomment'>X</a><div class='commentcon'><div class='loadmask'></div><iframe id='commframe' width='100%' height='100%' frameborder='0' src='http://139.196.248.235:8089/wap/v3/jsp/newDetail_commt_h5.jsp?_=1483673174552&contid="+$commentId+"'></iframe></div>").after("<div class='masker'></div>");

        }
    }
}


$(document).ready(function() {
    TP.comment_module.init();
});
